package com.slipper.common.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.slipper.common.bo.Result;
import com.slipper.common.constants.Constants;

public class ResultUtil {
    /**
     * 返回保存结果消息
     */
    public static <T> Result<T> saveResult(Number result) {
        if (result != null && result.longValue() > 0) {
            return successResult("保存成功！");
        } else {
            return failureResult("保存失败！");
        }
    }

    /**
     * 返回保存结果消息
     */
    public static <T> Result<T> saveResult(Number result, T t) {
        if (result != null && result.longValue() > 0) {
            return successResult("保存成功！", t);
        } else {
            return failureResult("保存失败！");
        }
    }

    /**
     * @param result 受影响的行数
     * @param operate 操作类型
     */
    public static <T> Result<T> operateResult(Number result, String operate) {
        if (result != null && result.longValue() > 0) {
            return successResult(operate + "成功！");
        } else {
            return failureResult(operate + "失败！");
        }
    }
    /**
     * 返回删除结果消息
     */
    public static <T> Result<T> deleteResult(Number result) {
        if (result != null && result.longValue() > 0) {
            return successResult("删除成功！");
        } else {
            return failureResult("删除失败！");
        }
    }
    
    /**
     * 处理失败，返回错误消息
     */
    public static <T> Result<T> failureResult() {
        return failureResult("操作失败！");
    }

    /**
     * 处理失败，返回错误消息
     */
    public static <T> Result<T> failureResult(String message) {
        return failureResult(Constants.NO, message);
    }

    /**
     * 处理失败，返回错误消息
     */
    public static <T> Result<T> failureResult(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(Constants.NO);
        result.setMessage("操作失败！");
        return result;
    }

    /**
     * 处理失败，返回错误消息
     */
    public static <T> Result<T> failureResult(String code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(StringUtil.isBlank(message) ? "操作失败！" : message);
        return result;
    }

    /**
     * 处理成功，返回消息
     */
    public static <T> Result<T> successResult() {
        return successResult(null, null);
    }
    
    /**
     * 处理成功，返回消息
     */
    public static <T> Result<T> successResult(String message) {
        return successResult(message, null);
    }

    /**
     * 处理成功，返回消息
     */
    public static <T> Result<T> successDataResult(T data) {
        return successResult(null, data);
    }

    /**
     * 处理成功，返回消息
     */
    public static <T> Result<T> successResult(T data) {
        return successResult(null, data);
    }

    /**
     * 处理成功，返回消息
     */
    public static <T> Result<T> successResult(String message, T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(Constants.YES);
        result.setMessage(StringUtil.isBlank(message) ? "操作成功！" : message);
        return result;
    }

    /**
     * 分页返回信息
     */
    public static <T> Result<List<T>> successResult(PageInfo<T> pageInfo) {
        Result<List<T>> result = new Result<>();
        result.setData(pageInfo.getList());
        result.setPageNumber(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalRecord(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setCode(Constants.YES);
        result.setMessage("操作成功");
        return result;
    }

    public static void failureResult(HttpServletResponse response, String message) throws IOException {
        failureResult(response, Constants.NO, message);
    }
    
    public static void failureResult(HttpServletResponse response, String code, String message) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Headers", "erp-app-id");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Max-Age", "864000");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Headers",
                "Access-Control-Allow-Headers, " +
                "Access-Control-Allow-Methods, " +
                "Access-Control-Allow-Origin, " +
                "Access-Control-Max-Age, " +
                "Authorization, " +
                "Cache-Control, " +
                "Content-Type");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.append(ResultUtil.failureResult(code, message).toString());
        writer.close();
        response.flushBuffer();
    }

    public static String toErrorJson(String message) {
        return ResultUtil.failureResult(message).toString();
    }

    public static String toSuccessJson(String message) {
        return ResultUtil.successResult(message).toString();
    }

    public static String toJson(Result<?> result) {
        return result == null ? ResultUtil.failureResult("未知错误！").toString() : result.toString();
    }

    public static String toJson(Object object) {
        return ResultUtil.successResult(object).toString();
    }
}
